﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class EditCustomer1 : System.Web.UI.Page
    {
        Helper helper = new Helper();
        CustomerEntity customerEntity = new CustomerEntity();
        CustomerController customerController = new CustomerController();

        protected void Page_Load(object sender, EventArgs e)
        {
            
            BindCustomer();
            
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        private void BindCustomer() 
        {
            int customerId = Convert.ToInt32(Request.QueryString["customerId"]);
            customerEntity = customerController.FindCustomerByCustomerId(customerId);
            NameTextBox.Text = customerEntity.Name;
            ShortNameTextBox.Text = customerEntity.ShortName;
            AddressTextBox.Text = customerEntity.Address;
            PhoneTextBox.Text = customerEntity.Phone;
            FaxTextBox.Text = customerEntity.Fax;
            EmailTextBox.Text = customerEntity.Email;
            ContactPersonTextBox.Text = customerEntity.ContactPerson;
            ZipCodeTextBox.Text = customerEntity.ZipCode;
            NumberTextBox.Text = customerEntity.Number.ToString();
            CodeTextBox.Text = customerEntity.Code;
            RemarkTextBox.Text = customerEntity.Remark;
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            customerEntity = new CustomerEntity();
            customerEntity.Id = Convert.ToInt32(Request.QueryString["customerId"]);
            customerEntity.Name = NameTextBox.Text;
            customerEntity.ShortName = ShortNameTextBox.Text;
            customerEntity.Address = ShortNameTextBox.Text;
            customerEntity.Phone = PhoneTextBox.Text;
            customerEntity.Fax = FaxTextBox.Text;
            customerEntity.Email = EmailTextBox.Text;
            customerEntity.ContactPerson = ContactPersonTextBox.Text;
            customerEntity.ZipCode = ZipCodeTextBox.Text;

            bool result = customerController.UpdateCustomer(customerEntity);
            if (result == true)
            {
                Session["FlashMessage"] = "Success: Update Customer Successful!";
                Session["FlashMessageType"] = "success";
            }
            else
            {
                Session["FlashMessage"] = "Error: Update Customer unsuccessful!";
                Session["FlashMessageType"] = "error";
            }
        }



        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/CustomerList.aspx");
        }
    }
}